package cn.cloudplug.ui.test;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;

import butterknife.BindView;
import cn.cloudplug.core.delegate.ScentDelegate;
import cn.cloudplug.core.util.log.LogUtil;
import cn.cloudplug.ui.R;
import cn.cloudplug.ui.R2;

/**
 * Created by pc on 2018/1/18.
 */

public class UITestDelegate extends ScentDelegate implements AMapLocationListener, RouteSearch.OnRouteSearchListener {

    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    //Mark
    MarkerOptions markerOption;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    RouteSearch routeSearch ;
    @BindView(R2.id.map)
    MapView mapView;
    private Marker marker;

    @Override
    protected Object setContent() {
        return R.layout.delegate_uitest;
    }

    @Override
    protected void onBindView(Bundle savedInstanceState, View view) {
        markerOption = new MarkerOptions();
        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(),R.mipmap.icon_littlepoint)));
        // 将Marker设置为贴地显示，可以双指下拉地图查看效果
        markerOption.setFlat(true);//设置marker平贴地图效果
        //高德地图生命周期
        mapView.onCreate(savedInstanceState);

        //隐藏缩放按钮
        mapView.getMap().getUiSettings().setZoomControlsEnabled(false);

        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();

        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);


    //获取一次定位结果：
    //该方法默认为false。
        mLocationOption.setOnceLocation(true);

    //获取最近3s内精度最高的一次定位结果：
    //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);

    //初始化定位
        mLocationClient = new AMapLocationClient(getContext().getApplicationContext());
    //设置定位回调监听
        mLocationClient.setLocationListener(this);
        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.Transport);
        if(null != mLocationClient){
            mLocationClient.setLocationOption(mLocationOption);
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient.stopLocation();
            mLocationClient.startLocation();
        }

        marker= mapView.getMap().addMarker(markerOption);

        routeSearch = new RouteSearch(getContext());
        routeSearch.setRouteSearchListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mapView!=null)
        mapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();

        if(mapView!=null)
            mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();

        if(mapView!=null)
            mapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {

        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
        //可在其中解析amapLocation获取相应内容。
                mapView.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(amapLocation.getLatitude(),amapLocation.getLongitude()),14));
                marker.setPosition(new LatLng(amapLocation.getLatitude(),amapLocation.getLongitude()));
                //初始化query对象，fromAndTo是包含起终点信息，walkMode是步行路径规划的模式
                RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(new RouteSearch.FromAndTo(new LatLonPoint(amapLocation.getLatitude(),amapLocation.getLongitude()),new LatLonPoint(amapLocation.getLatitude()+50,amapLocation.getLongitude())), RouteSearch.WALK_DEFAULT);
                routeSearch.calculateWalkRouteAsyn(query);//开始算路
            }else {
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                LogUtil.e("AmapError","location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }

    //RouteSearchListener

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
        //解析result获取算路结果，可参考官方demo

    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }
}
